package mapper

import (
	"context"
	"gorm.io/gorm"
	"mall/services/product/rpc/internal/domain/bo"
	"mall/services/product/rpc/internal/initialize/mysql"
	"mall/services/product/rpc/internal/mapper/entity"
)

var spuAttrValueAllColumn = "spu_attr_value_id, spu_id, attr_id, attr_name, attr_value_id, attr_value_name, attr_desc"

type SpuAttrValueMapper struct {
}

func NewSpuAttrValueMapper() *SpuAttrValueMapper {
	return &SpuAttrValueMapper{}
}

func (m *SpuAttrValueMapper) SelectCountCtx(ctx context.Context, db *gorm.DB, spuBO bo.SpuAttrValueBO) (totalCount int64, err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SpuAttrValueArrMapper#SelectCountCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := db.Session(&gorm.Session{SkipDefaultTransaction: true})
	var total int64
	userEntity := entity.SpuAttrValueEntity{}
	tx = tx.Select(spuAllColumn).Table(userEntity.TableName())
	m.getAllWhereSql(tx, spuBO)
	tx = tx.Count(&total)
	return total, tx.Error
}
func (m *SpuAttrValueMapper) SelectOneCtx(ctx context.Context, db *gorm.DB, spuBO bo.SpuAttrValueBO) (spuAttrValueEntity entity.SpuAttrValueEntity, err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SpuAttrValueArrMapper#SelectOneCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := db.Session(&gorm.Session{SkipDefaultTransaction: true})
	m.getAllWhereSql(tx, spuBO)
	var resp entity.SpuAttrValueEntity
	userEntity := entity.SpuAttrValueEntity{}
	tx = tx.Select(spuAttrValueAllColumn).Table(userEntity.TableName()).First(&resp)
	return resp, tx.Error
}
func (m *SpuAttrValueMapper) SelectListCtx(ctx context.Context, db *gorm.DB, spuBO bo.SpuAttrValueBO) (spuAttrValueEntity []entity.SpuAttrValueEntity, err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SpuAttrValueArrMapper#SelectListCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := db.Session(&gorm.Session{SkipDefaultTransaction: true})
	var resp []entity.SpuAttrValueEntity
	userEntity := entity.SpuAttrValueEntity{}
	tx = tx.Select(spuAttrValueAllColumn).Table(userEntity.TableName())
	m.getAllWhereSql(tx, spuBO)
	tx = tx.Find(&resp)
	return resp, tx.Error
}

func (m *SpuAttrValueMapper) getAllWhereSql(db *gorm.DB, spuAttrBO bo.SpuAttrValueBO) {
	if spuAttrBO.SpuAttrValueId > 0 {
		db.Where("spu_attr_value_id = ?", spuAttrBO.SpuAttrValueId)
	}
	if spuAttrBO.SpuId > 0 {
		db.Where("spu_id = ?", spuAttrBO.SpuId)
	}
	if spuAttrBO.AttrId > 0 {
		db.Where("attr_id = ?", spuAttrBO.AttrId)
	}
	if len(spuAttrBO.AttrName) > 0 || spuAttrBO.AttrName != "" {
		db.Where("attr_name = ?", spuAttrBO.AttrName)
	}
	if spuAttrBO.AttrValueId > 0 {
		db.Where("attr_value_id = ?", spuAttrBO.AttrValueId)
	}
	if len(spuAttrBO.AttrValueName) > 0 || spuAttrBO.AttrValueName != "" {
		db.Where("attr_value_name = ?", spuAttrBO.AttrValueName)
	}
	if len(spuAttrBO.AttrDesc) > 0 || spuAttrBO.AttrDesc != "" {
		db.Where("attr_desc = ?", spuAttrBO.AttrDesc)
	}
}

func (m *SpuAttrValueMapper) BatchInsert(ctx context.Context, db *gorm.DB, arr []entity.SpuAttrValueEntity) (idArr []int64, err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SpuAttrValueMapper#BatchInsert")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := db.Create(&arr)
	var ids []int64
	for _, v := range arr {
		ids = append(idArr, v.SpuAttrValueId)
	}
	return ids, tx.Error
}

func (m *SpuAttrValueMapper) DeleteCtx(ctx context.Context, engin *gorm.DB, id []int64) (err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SpuMapper#DeleteCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	spuAttrValueEntity := entity.SpuAttrValueEntity{}
	// db.Where("id in ?", []int{18, 19, 20}).Delete(&TestUser{})
	tx := engin.Delete(&spuAttrValueEntity, id)
	return tx.Error
}
